﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace DAL
{
   public class ProjectTypeDAL
    {
        SqlConnection str = new SqlConnection("Data Source=PC201609040948;Initial Catalog=TianTianDai_Db;Persist Security Info=True;User ID=sa;pwd=123456");
        /// <summary>
        /// 发标
        /// </summary>
        /// <returns></returns>
        public List<ProjectType> ShowProjectType()
        {
            str.Open();
            string sql = "select * from ProjectType";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            str.Close();
            List<ProjectType> listProjectType = new List<ProjectType>();
            foreach (DataRow item in ta.Rows)
            {
                ProjectType t = new ProjectType();
                t.ProjectTypeId = Convert.ToInt32(item["ProjectTypeId"]);
                t.ProjectTypeName = item["ProjectTypeName"].ToString();
            }
            return listProjectType;
        }
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteProjectType(int id)
        {
            str.Open();
            string sql = "delete ProjectType where ProjectTypeId='" + id + "'";
            SqlCommand com = new SqlCommand(sql, str);
            var i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public int ProjectType_Add(Model.ProjectType p)
        {
            str.Open();
            string sql = "insert into ProjectType values('" + p.ProjectTypeName + "')";
            SqlCommand com = new SqlCommand(sql, str);
            var i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 显示详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProjectType ShowProjectTypeX(int id)
        {
            str.Open();
            string sql = "select * from ProjectType where ProjectTypeId='" + id + "'";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            str.Close();
            List<ProjectType> listProjectType = new List<ProjectType>();
            foreach (DataRow item in ta.Rows)
            {
                ProjectType t = new ProjectType();
                t.ProjectTypeId = Convert.ToInt32(item["ProjectTypeId"]);
                t.ProjectTypeName = item["ProjectTypeName"].ToString();
            }
            return listProjectType.FirstOrDefault();
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public int UpdateProjectType(ProjectType data)
        {
            str.Open();
            string sql = "update signs set  ProjectTypeName='" + data.ProjectTypeName + "'";
            SqlCommand com = new SqlCommand(sql, str);
            int i = com.ExecuteNonQuery();
            return i;
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public List<ProjectType> SeleteProjectType(string name)
        {
            str.Open();
            string sql = "selete ProjectTypeId,ProjectTypeName from Project where ProjectTypeName like '" + name + "'";
            SqlDataAdapter adap = new SqlDataAdapter(sql, str);
            DataTable ta = new DataTable();
            adap.Fill(ta);
            List<ProjectType> listProjectType = new List<ProjectType>();
            foreach (DataRow item in ta.Rows)
            {
                ProjectType t = new ProjectType();
                t.ProjectTypeId = Convert.ToInt32(item["ProjectTypeId"]);
                t.ProjectTypeName = item["ProjectTypeName"].ToString();
            }
            return listProjectType;
        }
    }
}
